package com.fish.common.tag;

import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;

public class MyElFunction {

	private static Pattern ipMask = null;
	private static Pattern phoneMask = null;
	private static Pattern addressMask = null;

	static {
		ipMask = Pattern.compile("(\\d{1,3}\\.\\d{1,3}\\.)\\d{1,3}\\.\\d{1,3}");
		phoneMask = Pattern.compile("(\\d{3})\\d{4}(\\d{4})");
		addressMask = Pattern.compile("\\d+");
	}

	public static String replaceAll(String source, String regex, String target) {
		if (source == null)
			return "";
		if (regex == null || regex.length() == 0)
			return source;

		return source.replaceAll(regex, target);
	}

	public static String ipMask(String source, String symbol) {
		if (symbol == null)
			symbol = "*";
		return ipMask.matcher(source).replaceAll("$1" + symbol + "." + symbol);
	}

	public static String phoneMask(String source, String symbol) {
		if (symbol == null)
			symbol = "*";
		return phoneMask.matcher(source).replaceAll(
				"$1" +symbol+symbol+symbol+symbol + "$2");
	}

	public static String addressMask(String source, String symbol) {
		if (symbol == null)
			symbol = "*";
		return addressMask.matcher(source).replaceAll(symbol);
	}

	public static String emailMask(String source) {
		if (source != null && source.lastIndexOf("@") > 0) {
			return "*****" + source.substring(source.lastIndexOf("@"));
		}
		return "";
	}
	
	public static String maskNo(String source){
		if(source==null)return "";
		if(source.length()<4)return source;
		return source.substring(0,source.length()-4)+"**"+source.substring(source.length()-2);
	}
	
	public static String maskName(String source){
		if(source==null||source.length()<1)return source;
		String start=source.substring(0,1);
		String end=source.substring(1);
		StringBuilder mask=new StringBuilder();
		for(int i=0;i<end.length();i++){
			mask.append("*");
		}
		return start+mask.toString();
	}
	
	public static String encodingCn(String inputStr){
		try {
			return java.net.URLEncoder.encode(inputStr, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return inputStr;
	}
	
	public static String convertStatus(String statusIntValue){
		if("1".equals(statusIntValue)){
			return "通过";
		}else if("-1".equals(statusIntValue)){
			return "未审核";
		}else if("-5".equals(statusIntValue)){
			return "未通过";
		}else if("-9".equals(statusIntValue)){
			return "已删除";
		}
		return statusIntValue;
	}
	
	public static String convertBoolean(String booleanIntValue){
		if("1".equals(booleanIntValue)){
			return "是";
		}else{
			return "否";
		}
	}
	
	public static String truncateStr(String inputStr, Integer len){
		if(inputStr==null || "".equals(inputStr)){
			return "";
		}
		if(inputStr.length()>len){
			inputStr=inputStr.substring(0, len)+"......";
		}
		return inputStr;
	}
	
	public static String encodingHtml(String inputHtml){
		if(inputHtml==null || "".equals(inputHtml)){
			return "";
		}
		inputHtml = inputHtml.replace(">", "&gt;");
		inputHtml = inputHtml.replace("<", "&lt;");
		inputHtml = inputHtml.replace("  ", " &nbsp;");
		inputHtml = inputHtml.replace("  ", " &nbsp;");
		inputHtml = inputHtml.replace("\"", "&quot;");
		inputHtml = inputHtml.replace("\'", "&#39;");
		inputHtml = inputHtml.replace("\r\n", "<br/> ");
        return inputHtml;
	}
	
	public static String decodingHtml(String inputHtml){
		if(inputHtml==null || "".equals(inputHtml)){
			return "";
		}
		inputHtml = inputHtml.replace("&gt;", ">");
		inputHtml = inputHtml.replace("&lt;", "<");
		inputHtml = inputHtml.replace("&nbsp;", " ");
        inputHtml = inputHtml.replace(" &nbsp;", "  ");
        inputHtml = inputHtml.replace("&quot;", "\"");
        inputHtml = inputHtml.replace("&#39;", "\'");
        inputHtml = inputHtml.replace("<br/> ", "\r\n");
        return inputHtml;
	}
	
	public static void main(String[] args) {
		System.out.println("~~~~~~~~~~~~~~~~~~~~~~"+MyElFunction.phoneMask("13671679923","*"));
	}
	
}
